package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.zjcode.cdcer.enums.CommonEnum;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.model.CerSysteminitImportOfficialInfo;
import com.zjcode.cdcer.service.CerSysteminitImportOfficialInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/v1/cerSysteminitImportOfficialInfo/")
public class CerSysteminitImportOfficialInfoController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CerSysteminitImportOfficialInfoService cerSysteminitImportOfficialInfoService;

    @PostMapping("insertOfficeInfo")
    @ApiOperation("存入正式库")
    public JsonObj insertOfficeInfo(@RequestBody PageData pageData) {
        return cerSysteminitImportOfficialInfoService.insertOfficeInfo(pageData);
    }

    @PostMapping("listAllTableData")
    @ApiOperation("获取所有的初始数据正式库信息列表")
    public ListResult<Object> listAllTableData(@RequestBody PageData pageData) {
        return ResultBuilder.buildListSuccess(cerSysteminitImportOfficialInfoService.listAllTableData(pageData));
    }

    @GetMapping("findDataById")
    @ApiOperation("根据id获取详细信息")
    public JsonObj findDataById() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        CerSysteminitImportOfficialInfo info = cerSysteminitImportOfficialInfoService.findDataById(id);
        if (info != null) {
            Map<String, Object> data = new HashMap<>();
            data.put(CommonEnum.RESP.getValue(), info);
            return JsonObj.success(data);
        } else return JsonObj.error("未能获取到对应的数据");
    }

    @PostMapping("queryAllExportData")
    @ApiOperation("导出初始正式库数据")
    public JsonObj queryAllExportData(@RequestBody PageData pageData,
                                      HttpServletResponse response) {
        List<LinkedHashMap<String, Object>> list = cerSysteminitImportOfficialInfoService.queryAllExportDataBy(
                pageData);
        Map<String, Object> map = new HashedMap<>();
        map.put("data", list);
        return JsonObj.success(map);
    }

    @GetMapping("getNoErrorOfficeData")
    @ApiOperation("把cer_systeminit_import_official_info这个表数据存到cer_detaildata_import_info表里")
    public void getNoErrorOfficeData() {
        cerSysteminitImportOfficialInfoService.getNoErrorOfficeData();
    }


    @PostMapping("exportInitOfficialForGJMode")
    @ApiOperation("导出初始正式库(国家系统导出模板)")
    public void exportInitOfficialForGJMode(HttpServletResponse response) {
        PageData pageData = getPageData();
        cerSysteminitImportOfficialInfoService.exportInitOfficialForGJMode(pageData,response);
    }
}
